<!--
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-08-28 16:21:26
 * @LastEditTime : 2020-02-12 15:59:23
 * @LastEditors  : Please set LastEditors
 -->
<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>Hello APP</title>
    <link rel="stylesheet" type="text/css" href="../../css/vant.css">
    <link rel="stylesheet" type="text/css" href="../../css/base.css" />
    <script type="text/javascript" src="../../script/jquery.js"></script>
    <script type="text/javascript" src="../../script/layer/mobile/layer.js"></script>
    <script type="text/javascript" src="../../script/vue.min.js"></script>
    <script type="text/javascript" src="../../script/vant.min.js"></script>
    <script type="text/javascript" src="../../script/fastclick.js"></script>
    <script type="text/javascript" src="../../script/base.js"></script>
    <style type="text/css">
        .content {
            background: #fff !important;
        }

        .van-cell {
            line-height: 40px;
        }

        .van-cell__title {
            max-width: initial;
        }

        .pay_image {
            margin-right: 20px;
            width: 24px;
        }

        .money {
            text-align: center;
            margin-bottom: 20px;
            color: #7d7e80;
        }
    </style>
</head>

<body>
    <div id="app" v-cloak>
        <div class="init-full">
            <van-nav-bar class="head" title="支付" left-arrow @click-left="onClickLeft"></van-nav-bar>
            <div class="body-no-bottom">
                <div class="content">
                    <div style="padding: 70px 10px 0;">
                        <div class="money">您当前需支付金额{{payData.S_DMoney}}元</div>
                        <van-radio-group v-model="payData.S_PayType">
                            <van-cell-group>
                                <van-cell clickable @click="payData.S_PayType = '1'">
                                    <div slot="title"><img src="../../image/zfb.png" class="pay_image" />支付宝</div>
                                    <van-radio slot="right-icon" name="1" />
                                </van-cell>
                                <van-cell clickable @click="payData.S_PayType = '2'">
                                    <div slot="title"><img src="../../image/wx.png" class="pay_image" />微信</div>
                                    <van-radio slot="right-icon" name="2" />
                                </van-cell>

                            </van-cell-group>
                        </van-radio-group>
                    </div>
                    <div class="btn-padding">
                        <van-button type="primary" class="login-out" :disabled="ifConfirm" :loading="ifConfirm"
                            loading-text="支付中..." block @click="payOrder">确认支付</van-button>
                    </div>
                </div>
                <van-loading v-if="showLoading" />
            </div>
        </div>
    </div>
</body>

<script type="text/javascript">
    var vm;
    apiready = function () {
        //$('.init-full').show();

        
        var aliPayPlus = api.require('aliPayPlus');
        var wxPay = api.require('wxPay');
        var payData = api.pageParam.payData;
        var payType = api.pageParam.payType;
        vm = new Vue({
            el: '#app',
            data: {
                showLoading: false,
                ifConfirm: false,
                payData: payData,
                payType: payType
            },
            created: function () {

            },
            methods: {
                payOrder() {
                    var that = this;
                    if (that.payData.S_PayType == '0') {
                        layer.open({
                            content: '请选择支付方式',
                            skin: 'msg',
                            time: 2
                        });
                        return false;
                    }
                    that.ifConfirm = true;

                    FLD.ajax('AP_CHKPayN', that.payData, function (res) {
                        if (res.ds[0].status == 200) {
                            if (that.payType == 1) {
                                if (that.payData.S_PayType == '1') {
                                    FLD.nativeRequest_pay('pay', that.payData, function (res) {
                                        aliPayPlus.payOrder({
                                            orderInfo: res
                                        }, function (ret, err) {
                                            if (ret.code == '6001') {
                                                that.ifConfirm = false;
                                            } else {
                                                var obj = {
                                                    S_Type: 6,
                                                    S_BillType: that.payData.S_BillType,
                                                    S_BillID: that.payData.S_BillID
                                                }
                                                FLD.ajax('AP_WLPayZTCL', obj, function (res) {
                                                    api.sendEvent({
                                                        name: 'reloadForm',
                                                        extra: {
                                                            formID: that.payData.S_BillID
                                                        }
                                                    });
                                                    api.sendEvent({
                                                        name: 'refreshList',
                                                        extra: {
                                                            type: 2
                                                        }
                                                    });
                                                    api.sendEvent({
                                                        name: 'refreshList',
                                                        extra: {
                                                            type: 3
                                                        }
                                                    });
                                                    setTimeout(function () {
                                                        api.closeWin();
                                                    }, 2000)
                                                })
                                            }
                                        });
                                    })

                                } else {

                                    FLD.nativeRequest_pay('pay', that.payData, function (res) {

                                        var data = JSON.parse(res);

                                        var myDate = new Date();
                                        var now = Date.parse(new Date()) / 1000;

                                        var stringA = 'appid=' + data.appid + '&noncestr=' + data.nonce_str + '&package=Sign=WXPay' + '&partnerid=' + data.mch_id + '&prepayid=' + data.prepay_id + '&timestamp=' + now + '&key=fldabcdefg123456789erfgiht56jh9f';
                                        var signature = api.require('signature');
                                        signature.md5({
                                            data: stringA
                                        }, function (ret, err) {
                                            if (ret.status) {
                                                var sign = ret.value.toUpperCase();
                                                wxPay.payOrder({
                                                    apiKey: data.appid,
                                                    orderId: data.prepay_id,
                                                    mchId: data.mch_id,
                                                    nonceStr: data.nonce_str,
                                                    timeStamp: now,
                                                    package: 'Sign=WXPay',
                                                    sign: sign
                                                }, function (ret, err) {
                                                    if (err && err.code == -2) {
                                                        that.ifConfirm = false;
                                                    } else {
                                                        var obj = {
                                                            S_Type: 6,
                                                            S_BillType: that.payData.S_BillType,
                                                            S_BillID: that.payData.S_BillID
                                                        }
                                                        FLD.ajax('AP_WLPayZTCL', obj, function (res) {
                                                            api.sendEvent({
                                                                name: 'reloadForm',
                                                                extra: {
                                                                    formID: that.payData.S_BillID
                                                                }
                                                            });
                                                            api.sendEvent({
                                                                name: 'refreshList',
                                                                extra: {
                                                                    type: 2
                                                                }
                                                            });
                                                            api.sendEvent({
                                                                name: 'refreshList',
                                                                extra: {
                                                                    type: 3
                                                                }
                                                            });
                                                            setTimeout(function () {
                                                                api.closeWin();
                                                            }, 2000)
                                                        })
                                                    }
                                                });

                                            }
                                        });


                                    })


                                }
                            }
                        } else {
                            that.ifConfirm = false;
                            api.sendEvent({
                                name: 'refreshList',
                                extra: {
                                    type: 2
                                }
                            });
                            api.sendEvent({
                                name: 'refreshList',
                                extra: {
                                    type: 3
                                }
                            });

                            layer.open({
                                content: res.ds[0].msg,
                                skin: 'msg',
                                time: 2,
                                end: function () {
                                    api.sendEvent({
                                        name: 'reloadForm',
                                        extra: {
                                            formID: that.payData.S_BillID,
                                            time: 1000
                                        }
                                    });
                                    api.closeWin();
                                }
                            });

                        }
                    })






                },
                onClickLeft() {
                    api.closeWin();
                }
            }
        })



    }
</script>

</html>